<?php
	
	namespace Aspic\Cache;
	
	class PhpFileCache extends AbstractFileCache {

		public function __construct($performanceMode = true) {
			parent::__construct($performanceMode);
		}
		
		protected function getCache($key, $performanceMode) {
			$performanceMode = (is_null($performanceMode))?$this->opt('performanceMode'):$performanceMode;
			
			if ($performanceMode AND isset($this->memCache[$key])) {
				return $this->getMemCache($key);
			}
			else {
				$fp = $this->getCacheFilePath($key);

				if (is_file($fp)) {
					$ret = include $fp;

					return $ret;
				}
				else {
					return null;
				}
			}
			
		}
		
		/**
		 * 
		 * @param type $key
		 * @param type $data
		 * @param integer|string $expiresData A delay in second by which cache will be unvalided or 
		 * a file path for which filemtime() functions will determine if cache is still valid (when given file 
		 * is more recent than cache file)
		 * @return type
		 */
		public function cache($key, $data, $expiresData = 86400) {
			$fp = $this->getCacheFilePath($key);
			
			$this->setMemCache($key, $data, $expiresData);

			$toStore = array(
				'expiresData' => $expiresData, 
				'data' => $data,
			);
			
			$varExport = var_export($toStore, true);
			$php = '<?php return '.$varExport.'; ?>';
			
			return File\File::putContents($fp, $php);
		}
	   
   }